﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using DataBase.DBmodels;
using DataBase.Utils;
using GreenXi.Model;
using Microsoft.EntityFrameworkCore;
using Prism.Commands;

namespace GreenXi.ViewModel
{
    public class VirtualViewModel: CraftModel
    {
        private static int crafttype = 3; //组ID
        private Pages.Setting.VirtualEdit CraftEdit;
        public void Init()
        {
            using MyDbContext myDbContext = new();
            CraftList = new ObservableCollection<Craft>();
            var CraftLists = myDbContext.GetCraftByType(crafttype);
            foreach (var item in CraftLists)
            {
                CraftList.Add(item);
            }

        }

        public VirtualViewModel() {
            Init();//初始化
            Multi = new ObservableCollection<Help.UI.MultiComboBox.MultiCbxBaseData>();
            using MyDbContext myDbContext = new();
            var AllCraftLists = myDbContext.GetCraftByType(1);
            foreach (var item in AllCraftLists)
            {
                Multi.Add(new Help.UI.MultiComboBox.MultiCbxBaseData { ID = item.Id, Name = item.Project, IsCheck = false });
            }

            EditColumn = new DelegateCommand<object>((p) => { EditColumn_Click(p); });//编辑栏目
            SaveColumn = new DelegateCommand<object>((p) => { SaveColumn_Click(p); });//保存栏目
            CloseWindows = new DelegateCommand<object>((p) => { CloseWindows_Click(); });//关闭编辑窗口
            AddProject = new DelegateCommand(() => { AddProject_Click(); });          //新增项目
            DelProject = new DelegateCommand<object>((p) => { DelProject_Click(p); });//删除项目
            EditProject = new DelegateCommand<object>((p) => { EditProject_Click(p); });//编辑项目
            SaveProject = new DelegateCommand<object>((p) => { SaveProject_Click(p); });//保存项目
        }

        #region 项目
        private void CloseWindows_Click()
        {
            CraftEdit.Close();//关闭弹窗
        }
        private void EditColumn_Click(Object param)
        {
            var paramss = param as Craft;
            Crafts = paramss;
            CraftEdit = new Pages.Setting.VirtualEdit();


            foreach (var item in Multi)
            {
                if (Crafts.NodeId!=null&&Crafts.NodeId.Contains(item.ID + ","))
                {
                    item.IsCheck = true;
                }
                else
                {
                    item.IsCheck= false;
                }
            }

            CraftEdit.DataContext = this;
            CraftEdit.ShowDialog();
        }
        private void SaveColumn_Click(Object param)
        {
            using MyDbContext myDbContext = new();
            //查看选中电表
            String NodeId = "";
            if (Multi != null && Multi.Count > 0)
            {
                foreach (var item in Multi)
                {
                    if (item.IsCheck)
                    {
                        NodeId += item.ID + ",";
                    }
                }
            }
            Crafts.NodeId = NodeId;
            myDbContext.UpdateCraft(Crafts);
            GlobalVariables.CraftViewList = MyDbContext.GetCraftViewByType(5);
            MessageBox.Show("修改成功！");
            CraftEdit.Close();//关闭弹窗
        }
        private void EditProject_Click(Object param)
        {
            var paramss = param as Craft;
            Crafts = paramss;

           
            Pages.Setting.VirtualEdit CraftEdit = new();
            CraftEdit.DataContext = this;
            CraftEdit.ShowDialog();
        }

        // Swap 方法用于交换数组中两个位置的对象
        void Swap(List<Craft> array, int index1, int index2)
        {
            if (array == null)
            {
                return;
            }
            int tmpId = array[index1].Id;
            array[index1].Id = array[index2].Id;
            array[index2].Id = tmpId;
        }


        private void SaveProject_Click(Object param)
        {
            using MyDbContext myDbContext = new();
          
       
            myDbContext.UpdateCraft(Crafts);
        }
        private async void AddProject_Click()
        {
            var craft = new Craft
            {
                Project = "虚拟表名称",
                Speed = 50,
                StartPoint = GlobalVariables.GreatestPressure,
                EndPoint = 0,
                Type = crafttype,
                ShowType = "虚拟表"
            };
            CraftList.Add(craft);

            using MyDbContext myDbContext = new();
            await myDbContext.AddAsync<DataBase.DBmodels.Craft>(craft);

        }
        private async void DelProject_Click(object param)//删除项目
        {
            var paramss = param as Craft;
            using MyDbContext myDbContext = new();
            await myDbContext.DeleteAsync<Craft>(paramss);
            CraftList.Remove(paramss);
        }
        #endregion
    }
}

